//******************************************************************************
/**
 *
 * @file	field_flysky.c
 * @brief	フィールド　そらをとぶ
 * @author	kagaya
 * @data	05.07.13
 */
//******************************************************************************
#include "common.h"
#include "fieldsys.h"
#include "fieldmap.h"
#include "field_event.h"
#include "field_cutin.h"
#include "ev_mapchange.h"
#include "mapdata_warp.h"
#include "field/situation.h"
#include "situation_local.h"

#include "field_flysky.h"
#include "ev_pokemon.h"	//for	EvPoke_GetLivingPokeIdxTop
#include "system/snd_tool.h"

#include "pair_poke.h"	// for	PairPokeEvAnm_BallIn

//==============================================================================
//	define
//==============================================================================

//==============================================================================
//	typedef
//==============================================================================
//--------------------------------------------------------------
//	SORATOBI_WORK
//--------------------------------------------------------------
typedef struct
{
	FIELDSYS_WORK *fsys;
	u16 seq_no;
	u16 PairPokeUse;
	u16 zone_id;
	s16 map_gx;
	s16 map_gz;
	u16 sel_pos;		//金銀追加　空を飛ぶ使用ポケモンのリスト位置
	POKEMON_PARAM *poke_param;
	TCB_PTR tcb_cutin;
	TCB_PTR tcb_acmd;
}SORATOBI_WORK;

#define SORATOBI_WORK_SIZE (sizeof(SORATOBI_WORK))

//==============================================================================
//	フィールド　秘伝技　そらをとぶ
//==============================================================================
//--------------------------------------------------------------
/**
 * 秘伝技そらを飛ぶカットイン→マップ遷移イベント呼び出し用のワークセット
 * @param	heap_id		ワークを確保するHEAP ID
 * @param	fsys		FIELDSYS_WORK *
 * @param	param		表示するPOKEMON_PARAM *
 * @param	zone_id		遷移するゾーンID
 * @param	map_gx		遷移先出現位置グリッドX座標
 * @param	map_gz		遷移先出現位置グリッドZ座標
 * @retval	void*		確保したワーク*
 */
//--------------------------------------------------------------
void * EventWorkSet_FlySkyMapChange( u32 heap_id,
		FIELDSYS_WORK *fsys, POKEMON_PARAM *param, u16 sel_pos, u16 zone_id, s16 map_gx, s16 map_gz )
{
	SORATOBI_WORK *work = sys_AllocMemoryLo( heap_id, SORATOBI_WORK_SIZE );
	memset( work, 0, SORATOBI_WORK_SIZE );
	work->fsys = fsys;
	work->poke_param = param;
	work->zone_id = zone_id;
	work->map_gx = map_gx;
	work->map_gz = map_gz;
	work->sel_pos = sel_pos;
	return( work );
}

//--------------------------------------------------------------
/**
 * 空を飛ぶカットイン→マップ遷移
 * @param	event	GMEVENT_CONTROL *
 * @retval	BOOL	TRUE=終了
 */
//--------------------------------------------------------------
BOOL GMEVENT_FlySkyMapChange( GMEVENT_CONTROL *event )
{
	FIELDSYS_WORK *fsys = FieldEvent_GetFieldSysWork( event );
	SORATOBI_WORK *work = FieldEvent_GetSpecialWork( event );

	switch( work->seq_no ){
	case 0:
		//連れ歩きアニメ待ちイベントコール
		FieldEvent_Call(event, PairPokeEvAnm_WaitAnmEvent, NULL);
		work->seq_no++;
		break;
	case 1:
		{
			int player_form;
			player_form = Player_FormGet( fsys->player );
			if ((player_form == HERO_FORM_SWIM)||(player_form == HERO_FORM_CYCLE)){
				work->seq_no = 4;
				work->PairPokeUse = 0;
			}else{
				//連れ歩きが空を飛ぶを使うかをチェック
				if ( EvPoke_GetLivingPokeIdxTop(SaveData_GetTemotiPokemon(fsys->savedata))
						!=work->sel_pos ){
					//連れ歩き格納イベントコール
					PairPokeEvAnm_BallIn(work->fsys);

					work->seq_no = 4;
					work->PairPokeUse = 0;
				}else{
					u8 jump_num;
					//連れ歩き表示しているか？
					if ( PairPoke_IsVisible(fsys) ){
						if ( PairReact_IsSameType(fsys, HIKOU_TYPE) ){
							//ジャンプ2回
							jump_num = 2;
							//機嫌値+1
							PAIR_POKE_HUMOR_AddVal(fsys->PairPokeHumor, 1);
						}else{
							//ジャンプ1回
							jump_num = 1;
						}
						//ジャンプ
						PairReact_JumpAct(fsys, jump_num);
						work->PairPokeUse = 1;
						work->seq_no = 2;
					}else{
						//連れ歩いていない
						work->seq_no = 4;
						work->PairPokeUse = 0;
					}
				}
			}
		}
		break;
	case 2:
		//連れ歩きのときしかとおりません
		Snd_PMVoicePlay(
			PokeParaGet(work->poke_param,ID_PARA_monsno,NULL),
			PokeParaGet(work->poke_param,ID_PARA_form_no,NULL));
		work->seq_no++;
		break;
	case 3:
		//連れ歩きのときしかとおりません
		if ( Snd_PMVoicePlayCheck() ){
			break;
		}
		work->seq_no++;
		break;
	case 4:
		//主人公正面向きアニメコール　ここで合流
		{
			static const FIELD_OBJ_ACMD_LIST DATA_TurnAcmdTbl[] = {
				{ AC_ANM_PAUSE_ON, 1 },
				{ AC_STAY_WALK_D_8F, 1 },
				{ AC_ANM_PAUSE_OFF, 1 },
				{ ACMD_END, 0 },
			};

			work->tcb_acmd = FieldOBJ_AcmdListSet(
								Player_FieldOBJGet( fsys->player ),DATA_TurnAcmdTbl );
		}
		work->seq_no++;
		break;
	case 5:
		//アニメ終了待ち
		if ( FieldOBJ_AcmdListEndCheck(work->tcb_acmd) == FALSE ){
			break;
		}
		FieldOBJ_AcmdListEnd( work->tcb_acmd );

		//連れ歩きが空を飛ぶを使うかをチェック
		if ( !work->PairPokeUse ){
			work->tcb_cutin = FieldCutIn_Init(
				work->fsys, CUTIN_TYPE_SKY,
				work->poke_param, Player_SexGet(work->fsys->player) );
		}else{
			work->tcb_cutin = FieldCutIn_Init(
				work->fsys, CUTIN_TYPE_PAIR_SKY,
				work->poke_param, Player_SexGet(work->fsys->player) );
		}
		work->seq_no++;
		break;
	case 6:
		if( FieldCutIn_EndCheck(work->tcb_cutin) == FALSE ){
			break;
		}

		FieldCutIn_End( work->tcb_cutin );

		{
			u16 warp_id;
			LOCATION_WORK loc;
			SITUATION * sit = SaveData_GetSituation(fsys->savedata);

			warp_id = WARPDATA_SearchForFly(work->zone_id);
			GF_ASSERT(warp_id != 0);
			WARPDATA_GetWarpLocation(warp_id, &loc);

			//エスケープポイントを再設定
			WARPDATA_GetEscapeLocation(warp_id,Situation_GetEscapeLocation(sit));

			EventChange_MapChangeBySky( event,
					loc.zone_id, DOOR_ID_JUMP_CODE,
					loc.grid_x, loc.grid_z, DIR_DOWN );

		}

		sys_FreeMemoryEz( work );
	}

	return( FALSE );
}
